package Time_Clock;

import javax.swing.*;
import java.awt.*;

public class StillClock extends JFrame {
    private static Time time;
    private static Dimension dimension = new Dimension(300, 400);
    private static JFrame jFrame;

    public static void main(String[] args) {
        jFrame = new StillClock();

        StillClock.Timetextfield hourtext = new StillClock.Timetextfield("Hour", 2);
        StillClock.Timetextfield minutetext = new StillClock.Timetextfield("Minute", 2);
        StillClock.Timetextfield secondtext = new StillClock.Timetextfield("Second", 2);

        Container container = new Container();
        container.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 320));
        container.add(hourtext.jLabel);
        container.add(hourtext);
        container.add(minutetext.jLabel);
        container.add(minutetext);
        container.add(secondtext.jLabel);
        container.add(secondtext);

        jFrame.setContentPane(container);
        jFrame.setTitle("StillClock");
        jFrame.setVisible(true);
        jFrame.setBounds(800, 300, jFrame.getSize().width, jFrame.getSize().height);
        jFrame.setSize(jFrame.getSize().width, jFrame.getSize().height);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        super.paint(g);
        int centerX = getSize().width / 2;
        int centerY = getSize().height / 2;
        int radius = (int) (Math.min(getSize().width, getSize().height) * 0.8 * 0.5);

        g.setColor(Color.BLACK);
        g.drawLine(0, 350, 600, 350);
        g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
        g.setFont(new Font("Tahoma", Font.PLAIN, 20));
        g.drawString("3", centerX + radius - 10, centerY + 10);
        g.drawString(" 6", centerX - 10, centerY + radius);
        g.drawString("9", centerX - radius, centerY + 10);
        g.drawString("12", centerX - 10, centerY - radius + 20);

        int sLength = (int) (radius * 0.8);
        int secondX = (int) (centerX + sLength * Math.sin(getSecond() * (2 * Math.PI / 60)));
        int secondY = (int) (centerY - sLength * Math.cos(getSecond() * (2 * Math.PI / 60)));
        g.setColor(Color.RED);
        g.drawLine(centerX, centerY, secondX, secondY);

        int mLength = (int) (radius * 0.65);
        int minuteX = (int) (centerX + mLength * Math.sin(getMinute() * (2 * Math.PI / 60)));
        int minuteY = (int) (centerY - mLength * Math.cos(getMinute() * (2 * Math.PI / 60)));
        g.setColor(Color.BLUE);
        g.drawLine(centerX, centerY, minuteX, minuteY);

        int hLength = (int) (radius * 0.5);
        int hourX = (int) (centerX + hLength * Math.sin((getHour() % 12 + getMinute() / 60.0) * (2 * Math.PI / 12)));
        int hourY = (int) (centerY - hLength * Math.cos((getHour() % 12 + getMinute() / 60.0) * (2 * Math.PI / 12)));
        g.setColor(Color.GREEN);
        g.drawLine(centerX, centerY, hourX, hourY);
    }

    private StillClock() {
        time = new Time();
    }

    private StillClock(int hour, int minute, int second) {
        time = new Time(hour, minute, second);
    }

    private static int getHour() {
        return time.getHour();
    }

    private static void setHour(int hour) {
        time.setHour(hour);
    }

    private static int getMinute() {
        return time.getMinute();
    }

    private static void setMinute(int minute) {
        time.setMinute(minute);
    }

    private static int getSecond() {
        return time.getSecond();
    }

    private static void setSecond(int second) {
        time.setSecond(second);
    }

    private static void setCurrentTime() {
        time.setCurrentTime();
    }

    @Override
    public Dimension getSize() {
        return dimension;
    }

    static class Timetextfield extends JTextField {
        JLabel jLabel = new JLabel("");

        Timetextfield(String tds, int columns) {
            super(columns);
            jLabel.setText(tds);
            addActionListener(arg0 -> {
                int time;
                if (!getText().equals("")){
                    time = Integer.parseInt(getText());
                    switch (jLabel.getText()) {
                        case "Hour":
                            setHour(time % 12);
                            break;
                            case "Minute":
                                setMinute(time % 60);
                                break;
                                case "Second":
                                    setSecond(time % 60);
                                    break;
                    }
                }
                else
                    setCurrentTime();
                jFrame.repaint();
            });
        }
    }
}